Public Class CreditNotes
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents btnSave As System.Web.UI.WebControls.Button
    Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
    Protected WithEvents txtNumber As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtValue As System.Web.UI.WebControls.TextBox
    Protected WithEvents lblErr As System.Web.UI.WebControls.Label
    Protected WithEvents lblTitle As System.Web.UI.WebControls.Label
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator2 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator3 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents txtDescription As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtDate As System.Web.UI.WebControls.TextBox
    Protected WithEvents RequiredFieldValidator4 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents regvNumber As System.Web.UI.WebControls.RegularExpressionValidator
    Protected WithEvents txtSupplierCode As System.Web.UI.WebControls.TextBox
    Protected WithEvents reqvSupplierCode As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents hidSupplierID As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents lblCurrentPage As System.Web.UI.WebControls.Label
    Protected WithEvents hrefCNCreateNew As System.Web.UI.WebControls.HyperLink

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

#Region " Properties"

    Protected Property PageBO() As CCreditNotes
        Get
            Return CType(Session("MIMS_WRS.Web.CreditNotes.PageBO"), CCreditNotes)
        End Get
        Set(ByVal Value As CCreditNotes)
            Session("MIMS_WRS.Web.CreditNotes.PageBO") = Value
        End Set
    End Property

    Protected ReadOnly Property CreditNotesID() As Integer
        Get
            Try
                CreditNotesID = CInt(Request("Id"))
            Catch ex As Exception
                CreditNotesID = 0
            End Try
        End Get
    End Property

    Protected ReadOnly Property CustomerID() As Integer
        Get
            Try
                CustomerID = CInt(Request("UserId"))
            Catch ex As Exception
                CustomerID = 0
            End Try
        End Get
    End Property


    Private Property Url() As String
        Get
            Return CStr(ViewState("Url"))
        End Get
        Set(ByVal Value As String)
            ViewState("Url") = Value
        End Set
    End Property

    Private Property UrlReferrer() As String
        Get
            Return CStr(ViewState("UrlReferrer"))
        End Get
        Set(ByVal Value As String)
            ViewState("UrlReferrer") = Value
        End Set
    End Property
#End Region

    Protected WithEvents Header1 As Header

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Me.IsPostBack Then
            Security.InternalAndAdminOnly()
            Me.Url = Request.Url.PathAndQuery
            Me.UrlReferrer = Request.UrlReferrer.PathAndQuery
            'Assign header
            Header1.NavigationBar.Add("Back", AppRoot() & "/Customer/CreditNotesList.aspx?UserId=" + CustomerID.ToString())
            Header1.MenuIndex = EMenuIndex.InternalUserList

            Dim obj As New CCreditNotes
            If CreditNotesID <> 0 Then
                Try
                    obj.Load(CreditNotesID)
                    txtNumber.Text = obj.Number
                    txtDescription.Text = obj.Description
                    txtValue.Text = FormatNumber(obj.Value, 2, , , TriState.False).ToString()
                    txtDate.Text = FormatDate(obj.CreatedDate)
                    txtSupplierCode.Text = Business.Common.GetLKDValue(obj.SupplierCodeIdFk, 1)
                    hidSupplierID.Value = obj.SupplierCodeIdFk.ToString

                    lblTitle.Text = "UPDATE CREDIT NOTES"

                    'Submenu
                    lblCurrentPage.Text = "Update credit notes"
                    hrefCNCreateNew.Text = "Create New Credit Notes"
                    hrefCNCreateNew.NavigateUrl = "CreditNotes.aspx?UserId=" + Me.CustomerID.ToString

                Catch ex As Exception
                    Throw New ApplicationException("Failed to load user", ex)
                End Try
                If obj.CnIdPk <> CreditNotesID Then
                    Throw New ApplicationException("Credit Notes invalid. Credit Notes ID not match")
                End If
            Else
                obj.Init()
                lblTitle.Text = "CREATE NEW CREDIT NOTES"

                'Submenu
                lblCurrentPage.Text = "Create new credit notes"
                hrefCNCreateNew.Text = ""

                txtDate.Text = FormatDate(Date.Now)
                txtValue.Text = "0"
            End If
            Me.PageBO = obj
        End If
    End Sub


    Protected Sub UpdateData()
        With PageBO
            .Number = txtNumber.Text.Trim()
            .Description = txtDescription.Text.Trim()
            .Value = Decimal.Parse(txtValue.Text)
            '.CreatedDate = Date.Now
            .CreatedDate = Date.Parse(txtDate.Text.Trim())
            .CustomerIdFk = CustomerID
            .SupplierCodeIdFk = Integer.Parse(Me.hidSupplierID.Value)
        End With
    End Sub
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        UpdateData()
        PageBO.Save()
        Response.Redirect(AppRoot() & "/Customer/CreditNotesList.aspx?UserId=" + CustomerID.ToString())
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect(AppRoot() & "/Customer/CreditNotesList.aspx?UserId=" + CustomerID.ToString())
    End Sub
End Class
